package com.xjgzinfo.usdp.service;

import com.xjgzinfo.usdp.domain.BladeVisualDb;
import com.xjgzinfo.usdp.domain.BladeVisualDb_;
import com.xjgzinfo.usdp.jpa.service.QueryService;
import com.xjgzinfo.usdp.repository.BladeVisualDbRepository;
import com.xjgzinfo.usdp.service.dto.BladeVisualDbCriteria;
import com.xjgzinfo.usdp.service.dto.BladeVisualDbDTO;
import com.xjgzinfo.usdp.service.mapper.BladeVisualDbMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service for executing complex queries for {@link BladeVisualDb} entities in the database.
 * The main input is a {@link BladeVisualDbCriteria} which gets converted to {@link Specification},
 * in a way that all the filters must apply.
 * It returns a {@link List} of {@link BladeVisualDbDTO} or a {@link Page} of {@link BladeVisualDbDTO} which fulfills the criteria.
 */
@Service
@Transactional(readOnly = true)
public class BladeVisualDbQueryService extends QueryService<BladeVisualDb> {

    private final Logger log = LoggerFactory.getLogger(BladeVisualDbQueryService.class);

    private final BladeVisualDbRepository bladeVisualDbRepository;

    private final BladeVisualDbMapper bladeVisualDbMapper;

    public BladeVisualDbQueryService(BladeVisualDbRepository bladeVisualDbRepository, BladeVisualDbMapper bladeVisualDbMapper) {
        this.bladeVisualDbRepository = bladeVisualDbRepository;
        this.bladeVisualDbMapper = bladeVisualDbMapper;
    }

    /**
     * Return a {@link List} of {@link BladeVisualDbDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public List<BladeVisualDbDTO> findByCriteria(BladeVisualDbCriteria criteria) {
        log.debug("find by criteria : {}", criteria);
        final Specification<BladeVisualDb> specification = createSpecification(criteria);
        return bladeVisualDbMapper.toDto(bladeVisualDbRepository.findAll(specification));
    }

    /**
     * Return a {@link Page} of {@link BladeVisualDbDTO} which matches the criteria from the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @param page     The page, which should be returned.
     * @return the matching entities.
     */
    @Transactional(readOnly = true)
    public Page<BladeVisualDbDTO> findByCriteria(BladeVisualDbCriteria criteria, Pageable page) {
        log.debug("find by criteria : {}, page: {}", criteria, page);
        final Specification<BladeVisualDb> specification = createSpecification(criteria);
        return bladeVisualDbRepository.findAll(specification, page)
                .map(bladeVisualDbMapper::toDto);
    }

    /**
     * Return the number of matching entities in the database.
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the number of matching entities.
     */
    @Transactional(readOnly = true)
    public long countByCriteria(BladeVisualDbCriteria criteria) {
        log.debug("count by criteria : {}", criteria);
        final Specification<BladeVisualDb> specification = createSpecification(criteria);
        return bladeVisualDbRepository.count(specification);
    }

    /**
     * Function to convert {@link BladeVisualDbCriteria} to a {@link Specification}
     *
     * @param criteria The object which holds all the filters, which the entities should match.
     * @return the matching {@link Specification} of the entity.
     */
    protected Specification<BladeVisualDb> createSpecification(BladeVisualDbCriteria criteria) {
        Specification<BladeVisualDb> specification = Specification.where(null);
        if (criteria != null) {
            if (criteria.getId() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getId(), BladeVisualDb_.id));
            }
            if (criteria.getName() != null) {
                specification = specification.and(buildStringSpecification(criteria.getName(), BladeVisualDb_.name));
            }
            if (criteria.getCreateDept() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateDept(), BladeVisualDb_.createDept));
            }
            if (criteria.getPassword() != null) {
                specification = specification.and(buildStringSpecification(criteria.getPassword(), BladeVisualDb_.password));
            }
            if (criteria.getUrl() != null) {
                specification = specification.and(buildStringSpecification(criteria.getUrl(), BladeVisualDb_.url));
            }
            if (criteria.getDriverClass() != null) {
                specification = specification.and(buildStringSpecification(criteria.getDriverClass(), BladeVisualDb_.driverClass));
            }
            if (criteria.getUsername() != null) {
                specification = specification.and(buildStringSpecification(criteria.getUsername(), BladeVisualDb_.username));
            }
            if (criteria.getCreateTime() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateTime(), BladeVisualDb_.createTime));
            }
            if (criteria.getRemark() != null) {
                specification = specification.and(buildStringSpecification(criteria.getRemark(), BladeVisualDb_.remark));
            }
            if (criteria.getUpdateUser() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getUpdateUser(), BladeVisualDb_.updateUser));
            }
            if (criteria.getCreateUser() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getCreateUser(), BladeVisualDb_.createUser));
            }
            if (criteria.getUpdateTime() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getUpdateTime(), BladeVisualDb_.updateTime));
            }
            if (criteria.getStatus() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getStatus(), BladeVisualDb_.status));
            }
            if (criteria.getIsDeleted() != null) {
                specification = specification.and(buildRangeSpecification(criteria.getIsDeleted(), BladeVisualDb_.isDeleted));
            }
        }
        return specification;
    }
}